package com.zheng.upms.rpc.service.impl;

import com.zheng.common.annotation.BaseService;

import com.zheng.common.util.WeixinUtil;
import com.zheng.upms.dao.model.UpmsOrganization;
import com.zheng.upms.dao.mapper.UpmsOrganizationMapper;
import com.zheng.upms.rpc.api.UpmsOrganizationService;
import com.zheng.common.base.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 组织 服务实现类
 * </p>
 *
 * @author likun
 * @since 2017-12-29
 */
@Service
@Transactional
@BaseService
public class UpmsOrganizationServiceImpl extends BaseServiceImpl<UpmsOrganizationMapper, UpmsOrganization> implements UpmsOrganizationService {
    public boolean insetOrganization(UpmsOrganization organization) {
        setPids(organization);
        organization.setCtime(new Date());
        insert(organization);
        return WeixinUtil.insertOrganization(organization.getOrganizationId(), organization.getName(), organization.getPid());
    }

    public boolean updateOrganization(Integer id, UpmsOrganization organization) {
        organization.setOrganizationId(id);
        setPids(organization);
        updateById(organization);
        return WeixinUtil.updateOrganization(organization.getOrganizationId(), organization.getName(), organization.getPid());
    }

    private void setPids(UpmsOrganization entity) {
        if (entity.getPid() == null || entity.getPid().equals(0)) {
            entity.setPid(0);
            entity.setPath("0,");
        } else {
            int pid = entity.getPid();
            UpmsOrganization pOrganization = selectById(pid);
            String path = pOrganization.getPath();
            entity.setPid(pid);
            entity.setPath(path + pid + ",");
        }
    }
}
